/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.entity;

import exceptions.*;
import java.util.Set;
import javax.ejb.Remote;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author François
 */
@Entity
@Remote(AccountRemote.class)
@Table(name = "account")
@NamedQueries({
	@NamedQuery(name = "Account.getAll", query = "SELECT a FROM Account a"),
	@NamedQuery(name = "Account.connect", query = "SELECT a FROM Account a WHERE a.id = :paramId") })
public class Account implements AccountRemote {
    /**
     * Default serial version UID.
     */
    private static final long serialVersionUID = 1L;
    @Id
    //@Column(name = "iban_account", unique = true, nullable = false, length = 34)
    @Column(name = "iban_account", length = 34)
    private String iban;
    
    
    @Column(name = "solde_account")
    private double solde;

    
    @Column(name = "id_account")
    private String id;
    
    @Column(name = "motdepasse_account")
    private String motDePasse;

    @OneToMany(mappedBy = "account", fetch = FetchType.EAGER, targetEntity = Commande.class)
    private Set<CommandeRemote> commandes;
    
    public Account() {
    }

    public Account(String iban, double solde, Set<CommandeRemote> commandes, String id, String motDePasse) {
        this.iban = iban;
        this.id = id;
	this.commandes = commandes;
        this.solde = solde;
        this.motDePasse = motDePasse;
    }
     
    
    @Override
    public void deposit(double amount){
        solde += amount;
    }
    
    @Override
    public double withdraw(double amount) throws AccountException {
	double retour = 0.0;

	if (this.solde < amount) {
	    throw new AccountException(
		    "Oops! Impossible de retirer de l'argent sur le compte, solde insuffisant");
	}
	this.solde -= amount;
	retour = this.solde;

	return retour;
    }
    
    
    public static void verifierIban(String iban) throws IdentifiantInvalide {
	/*
	 * Identifiant renseigné
	 */
	if (iban == null) {
	    throw new IdentifiantInvalide(
		    "L'identifiant du Compte est obligatoire. null");
	}

	/*
	 * Identifiant de bonne taille
	 */
	if (iban.length() > 34) {
	    throw new IdentifiantInvalide(
		    "L'identifiant du Compte est trop long. " + iban);
	}
    }
    
    public static void verifierInstance(AccountRemote account)
	    throws InstanceInvalide, IdentifiantInvalide {
	if (account != null) {
	    /*
	     * Vérification iban
	     */
	    Account.verifierIban(account.getIban());
	} else {
	    throw new InstanceInvalide("L'instance de Compte est nulle");
	}
    }
    
    public double getBalance(){
        return solde;
    }
    
    
    public String getIban() {
        return iban;
    }

    public void setIban(String iban) {
        this.iban = iban;
    }

    public double getSolde() {
        return solde;
    }

    @Override
    public Set<CommandeRemote> getCommandes() {
	return this.commandes;
    }

    @Override
    public void setCommandes(Set<CommandeRemote> commandes) {
	for (CommandeRemote commande : commandes) {
	    commande.setCompte(this);
	}
	this.commandes = commandes;
    }
    
    public void setSolde(double solde) {
        this.solde = solde;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    

    public String getMotDePasse(){
        return motDePasse;
    }

    public void setMotDePasse(String motDePasse){
        this.motDePasse = motDePasse;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result
		+ ((commandes == null) ? 0 : commandes.hashCode());
	result = prime * result + ((iban == null) ? 0 : iban.hashCode());
	result = prime * result
		+ ((id == null) ? 0 : id.hashCode());
	long temp;
	temp = Double.doubleToLongBits(solde);
	result = prime * result + (int) (temp ^ (temp >>> 32));
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj) {
	    return true;
	}
	if (obj == null) {
	    return false;
	}
	if (!(obj instanceof Account)) {
	    return false;
	}
	Account other = (Account) obj;
	if (commandes == null) {
	    if (other.commandes != null) {
		return false;
	    }
	} else if (!commandes.equals(other.commandes)) {
	    return false;
	}
	if (iban == null) {
	    if (other.iban != null) {
		return false;
	    }
	} else if (!iban.equals(other.iban)) {
	    return false;
	}
	if (id == null) {
	    if (other.id != null) {
		return false;
	    }
	} else if (!id.equals(other.id)) {
	    return false;
	}
	if (Double.doubleToLongBits(solde) != Double
		.doubleToLongBits(other.solde)) {
	    return false;
	}
	return true;
    }

    @Override
    public String toString() {
	return "Compte [iban=" + iban + ", solde=" + solde + ", identifiant="
		+ id + ", commandes="
		+ commandes + "]";
    }
    
}
